package com.example.wechat.domain;

import java.util.List;
import java.util.Map;

/**
 * 分页类
 */
public class PageInfo {

	/**
	 * 每页的记录数
	 */
	private int pagesize = 10;

	/**
	 * 请求的页数
	 */
	private int currentPageNo = 0;

	/**
	 * 开始的记录数
	 */
	private int beginResult;

	/**
	 * 总记录数
	 */
	private int totalResult;

	/**
	 * 总页数
	 */
	private int totalPage;

	/**
	 * 开始的记录数 - 调用接口用offset
	 */
	private long offset;

	/**
	 * 根据获取的请求的页数与总记录数,计算得出相关参数
	 * 
	 * @param currentPageNo
	 * @param totalResult
	 */
	public PageInfo(int currentPageNo, int totalResult) {
		this(10, currentPageNo, totalResult);
	}

	public PageInfo(int pageSize, int currentPageNo, int totalResult) {
		if (pageSize == 0) {
			pageSize = 10;
		}
		if (currentPageNo == 0) {
			currentPageNo = 1;
		}
		this.pagesize = pageSize;
		this.currentPageNo = currentPageNo;
		this.totalResult = totalResult;
		if (this.currentPageNo == 0) {
			this.currentPageNo = 1;
		}
		if (totalResult <= this.pagesize) {
			this.beginResult = 0;
			this.currentPageNo = 1;
			this.totalPage = 1;
		} else {
			this.totalPage = this.totalResult / this.pagesize;
			if (this.totalResult % this.pagesize != 0) {
				this.totalPage += 1;
			}
			if (this.currentPageNo == 1) {
				this.beginResult = 0;
			} else {
				this.beginResult = (this.currentPageNo - 1) * this.pagesize;
			}
		}
	}

	public void setPagesize(int pagesize) {
		this.pagesize = pagesize;
	}

	public int getPagesize() {
		return pagesize;
	}

	public int getCurrentPageNo() {
		return currentPageNo;
	}

	public void setCurrentPageNo(int currentPageNo) {
		this.currentPageNo = currentPageNo;
	}

	public int getBeginResult() {
		return beginResult;
	}

	public void setBeginResult(int beginResult) {
		this.beginResult = beginResult;
	}

	public int getTotalResult() {
		return totalResult;
	}

	public void setTotalResult(int totalResult) {
		this.totalResult = totalResult;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public long getOffset() {
		return offset;
	}

	public void setOffset(long offset) {
		this.offset = offset;
	}

	public static List<Map<String, Object>> listForPage(int pageSize, int currentPage, int totalResult,
			List<Map<String, Object>> lists) {
		if (lists.size() >= 1) {
			lists = lists.subList((currentPage - 1) * pageSize,
					currentPage * pageSize > totalResult ? (totalResult) : currentPage * pageSize);
		}
		return lists;
	}

	public static List<String> stringListForPage(int pageSize, int currentPage, int totalResult, List<String> userIds) {
		if (userIds.size() >= 1) {
			userIds = userIds.subList((currentPage - 1) * pageSize,
					currentPage * pageSize > totalResult ? (totalResult) : currentPage * pageSize);
		}
		return userIds;
	}
}
